package com.itcast.mallapplet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.admin.vo.DityVO;
import com.itcast.applet.dto.DityPageRequest;
import com.itcast.applet.entity.Commodity;
import com.itcast.mallapplet.mapper.CommodityMapper;
import com.itcast.mallapplet.service.CommodityService;
import com.itcast.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommodityServiceImpl implements CommodityService{

    @Autowired
    private CommodityMapper commodityMapper;


    //根据分类id查询商品
    @Transactional
    public PageResult dityQuerylist(DityPageRequest dityPageRequest) {
        //调用工具PageHelper里面传页码和每页记录数
        PageHelper.startPage(dityPageRequest.getPage(), dityPageRequest.getPageSize());
        Page<Commodity> Page = commodityMapper.dityQuerylist(dityPageRequest);
        //这个集合里面的商品数据
        List<Commodity> result = Page.getResult();
        List<Long> commodityIdList = result.stream().map(Commodity::getId).collect(Collectors.toList());

        //根据商品id拿到所有数据
        List<DityVO> list = commodityMapper.getDityUrl(commodityIdList);
        return new PageResult(Page.getTotal(),list);
    }

    //根据id查询单个商品
    public Commodity dityQuery(Long id) {
        Commodity dity = commodityMapper.getById(id);
        return dity;
    }


}
